package com.campsys.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.campsys.domain.TCourseLearnUnit;
import com.campsys.domain.vo.ChapterListVo;
import com.campsys.domain.vo.SectionListVo;
import com.campsys.mapper.LearnUnitMapper;
import com.campsys.mapping.LearnUnitMapping;
import com.campsys.service.ILearnUnitService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class LearnUnitServiceImpl extends ServiceImpl<LearnUnitMapper, TCourseLearnUnit> implements ILearnUnitService {

    @Autowired
    private LearnUnitMapper learnUnitMapper;


    @Override
    public List<ChapterListVo> list(Long courseId) {

        LambdaQueryWrapper<TCourseLearnUnit> qw1 = new LambdaQueryWrapper<>();

        qw1.eq(TCourseLearnUnit::getIsChpt,1);
        qw1.eq(TCourseLearnUnit::getCourseId,courseId);
        qw1.ne(TCourseLearnUnit::getStatus,2);


        List<ChapterListVo> chapterListVos = LearnUnitMapping.INSTANCE.toChapters(learnUnitMapper.selectList(qw1));
        for (ChapterListVo chapterListVo : chapterListVos){
            LambdaQueryWrapper<TCourseLearnUnit> qw2 = new LambdaQueryWrapper<>();

            qw2.eq(TCourseLearnUnit::getIsChpt,0);
            qw2.eq(TCourseLearnUnit::getCourseId,courseId);
            qw2.ne(TCourseLearnUnit::getStatus,2);
            qw2.eq(TCourseLearnUnit::getFatherId,chapterListVo.getId());

            List<SectionListVo> sectionListVos = LearnUnitMapping.INSTANCE.toSections(learnUnitMapper.selectList(qw2));
            chapterListVo.setSectionListVos(sectionListVos);
        }

        return chapterListVos;
    }
}
